﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SQLite;
using System.Text;

namespace SqliteWrapper.Procedures
{
    public class Proc_UpdateCategory : IProcedure
    {
        public override SqliteError doProcedure(object DataSource,string DBPath)
        {
            SqliteConnectionManager manager = SqliteConnectionManager.Create(DBPath);
            SqliteError error = new SqliteError() { IsProcessSuccess = false, ErrorText = null };
            SqliteError bk_update_error = new SqliteError() { IsProcessSuccess = false, ErrorText = null };
            Category category = DataSource as Category;
            using (SQLiteWriteLock writeLocker = new SQLiteWriteLock(manager.ConnectionString))
            {
                using (SQLiteConnection conn = manager.GetSqliteConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "update Category set Title = @Title,Desc = @Desc where ID = @ID";
                        cmd.Parameters.Add("@ID", System.Data.DbType.Int64).Value = category.ID;
                        cmd.Parameters.Add("@Title", System.Data.DbType.String).Value = category.Title;
                        cmd.Parameters.Add("@Desc", System.Data.DbType.String).Value = category.Desc;
                        try
                        {
                            cmd.ExecuteNonQuery();
                            using (SQLiteTransaction trans = conn.BeginTransaction())
                            {
                                category.BookList.ForEach(book =>
                                {
                                    book.CategoryId = category.ID;
                                    using (SQLiteCommand subCmd = conn.CreateCommand())
                                    {
                                        subCmd.CommandText = "update Book set CategoryId = @CategoryId where ID = @ID";
                                        subCmd.Parameters.Add("@ID", System.Data.DbType.Int64).Value = book.ID;
                                        subCmd.Parameters.Add("@CategoryId", System.Data.DbType.String).Value = book.CategoryId;
                                        subCmd.Transaction = trans;
                                        subCmd.ExecuteNonQuery();
                                    }
                                });
                                try
                                {
                                    trans.Commit();
                                    bk_update_error.IsProcessSuccess = true;
                                }
                                catch (Exception ex)
                                {
                                    bk_update_error.ErrorText = ex.Message;
                                    trans.Rollback();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            error.ErrorText = ex.Message;
                        }
                    }
                }
            }
            if (!bk_update_error.IsProcessSuccess)
            {
                error.IsProcessSuccess = false;
                error.ErrorText = bk_update_error.ErrorText;
            }
            return error;
        }
    }
}
